<script setup>
import axios from 'axios';
import Add from '@/components/Menu/Add.vue';
import { onMounted, ref } from 'vue';
import { showMsg,showDialog} from './Util';
let optype = ref('new');
let title = ref('添加菜单');
let list = ref([]);
function getData(){
    axios.get('/api/menu_list').then(res =>{
        console.log(res);
        if(res.data.status==200){
            list.value = res.data.data;
            console.log(list)
        }else{
            showMsg("获取数据失败",'error',2000);
        }
    })
}

function newMenu(){
    addshow.value=false;
    getData();
}
function del(id){
    showDialog({msg:'确认删除?'}).then(res=>{
    axios.delete('/api/menu_delete/'+id).then(res=>{
        console.log(res);
        if(res.data.status==200){
            getData();
        }else{
            showMsg(res.data.msg,'error',2000);
        }
    })
})
}
function edit(id){
    //显示表单窗口
    //显示数据到窗口
   addshow.value = true;
   //list [{},{}]
   let obj = list.value.filter(item=>item.id==id);
   console.log(obj);
   menu.value = obj[0];
   optype.value = 'edit';
   title.value="修改菜单"

}
let menu = ref({title:'',icon:'',pid:'',auth:'',path:''})
let addshow=ref(false);
onMounted(() =>{
    getData();
})
function news(){
    addshow.value=true;
    title.value="添加菜单";
}
</script>
<template>
    <Add :title="title" :list2="list"  :type="optype" :menu="menu" v-show="addshow"@submit="newMenu" @close="addshow=false"></Add>
    <div class="page">
        <div class="tool">
            <button @click="news">添加</button>
        </div>
        <ul class="list">
            <li class="title">
                <span>菜单名称</span>
                <span>图表</span>
                <span>上级菜单</span>
                <span>角色</span>
                <span>路由</span>
                <span>按钮</span>
            </li>
            <li class="item" v-for="item in list" :key="item.id">
                <span>{{ item.title }}</span>
                <span>{{ item.icon }}</span>
                <span>{{ item.pid }}</span>
                <span>{{ item.auth }}</span>
                <span>{{ item.path }}</span>
                <span class="anniu">
                    <button @click="del(item.id)" class="ss">删除</button>
                    <button @click="edit(item.id)" class="ss">修改</button>
                </span>
            </li>
        </ul>
    </div>